<?php
namespace Plu\Library\Models\Platform\Rooms;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column,
    Phalcon\DI;

class Room extends Model {
    public $RoomId;//房间号

    public $UserId;//房主编号

    public $Name;//房间名

    public $Desc;//房间说明

    public $Logo;//房间Logo

    public $Status;//状态(1:正常  0:异常)

    public $UserTitle;//用户称号

    public $WeChatPic;//微信图片

    public $UserQQ;//qq号码

    public $RoomTitleKey;//房间标题key

    public $VerifiedInformation;//房间认证信息

    public $AnchorCertification;//主播前端认证资料

    public $AnchorRecommend;//主播推荐形式

    public $IsShowBackground;//是否展示主播前端背景

    public $AnchorBackground;//主播前端背景

    public $AnchorCategory;//主播分类

    public $HostInfo;//房主简介

    public $RoomPlayVideoType;//1-直播，2-点播，4-全部

    public $LivePermission;

    public $VideoPermission;

    public $GameId;

    public $CategoryId;//新版本使用

    const AnchorCategory = array(
        'Nothing'       => 0,
        'Tga'           => 1,
        'Profession'    => 2,
        'God'           => 3,
        'Beauty'        => 4,
        'Sign'          => 5,
        'Entertainment' => 6,
		'Music'         => 7
    );

    const AnchorCategoryText = array(
        self::AnchorCategory['Nothing']    => '无',
        self::AnchorCategory['Tga']        => 'TGAstar',
        self::AnchorCategory['Profession'] => '职业选手',
        self::AnchorCategory['God']        => '大神',
        self::AnchorCategory['Beauty']     => '美女星主播',
        self::AnchorCategory['Sign']       => '签约主播'
    );

    const RoomType = array(
        'LIVE'      => 1,
        'VIDEO'		=> 2,
        'ALL'		=> 4
    );

    const CACHE_TIME = 3600;

    public function getSource() {
        return "Room";
    }

    public function initialize() {
        $this->setReadConnectionService('db.platform.slave');
    }

    public static function getRoomsInfo($roomIds) {
        if (empty($roomIds)) {
            return false;
        }

        $rooms = self::query()
            ->inWhere('RoomId', $roomIds)
            ->execute();

        if (count($rooms) == 0) {
            return false;
        }

        $rooms = $rooms->toArray();

        return $rooms;
    }

    public static function getRoomInfo($roomId, $cache_time = self::CACHE_TIME) {
        if ($cache_time > 0) {
            $cache_handler = DI::getDefault()->get('cache');
            $guid = sprintf("room_%d", $roomId);
            $item = $cache_handler->get($guid, self::CACHE_TIME);
        }

        if (empty($item) || gettype($item) != 'array') {
            $item = self::findFirst(array(
                "RoomId = :roomId:",
                "bind" => array(
                    'roomId' => $roomId
                ),
                "bindTypes" => array(
                    'roomId' => Column::BIND_PARAM_INT
                )
            ));

            if ($item == false) {
                $item = [];
            } else {
                $item = $item->toArray();
            }

            if ($cache_time > 0) {
                $cache_handler->save($guid, $item);
            }
        }

        return $item;
    }

    public static function getRoomInfoByDomain($domain, $cache_time = self::CACHE_TIME) {
        if ($cache_time > 0) {
            $cache_handler = DI::getDefault()->get('cache');
            $guid = sprintf("room_domain_%d", $domain);
            $item = $cache_handler->get($guid, self::CACHE_TIME);
        }

        if (empty($item) || gettype($item) != 'array') {
            $item = self::findFirst(array(
                "Domain = :domain:",
                "bind" => array(
                    'domain' => $domain
                ),
                "bindTypes" => array(
                    'domain' => Column::BIND_PARAM_STR
                )
            ));

            if ($item == false) {
                $item = [];
            } else {
                $item = $item->toArray();
            }

            if ($cache_time > 0) {
                $cache_handler->save($guid, $item);
            }
        }

        return $item;
    }

    /**
     * 通过用户id 只查询有效的房间
     * @param int $userid
     * @param int $power 权限1：直播2：点播4：全部
     * @return mixed
     */
    public static function getRoomByUserId($uid, $type){
        $item = self::findFirst(array(
            "UserId = :uid: AND Status = 1",
            "bind"	=> array(
                'uid' => $uid
            ),
            "bindTypes" => array(
                'uid' => Column::BIND_PARAM_INT
            )
        ));

        if ($item == false) {
            return false;
        } else {
            if ($type == self::RoomType['VIDEO'] && ($item->VideoPermission) > 0) {
                return $item;
            } elseif ($type == self::RoomType['LIVE'] && ($item->LivePermission & 31) > 0) {// 直播权限
                return $item;
            }
        }

        return false;
    }

}
?>
